package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.ResponseResult;
import com.example.domain.entity.Plant;
import com.example.exception.FloraException;


import com.example.domain.entity.Plant;


import com.example.service.IPlantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zrr
 * @since 2022-12-01
 */
@RestController
@RequestMapping("/plant")
public class PlantController {
    @Autowired
    private IPlantService plantService;

    /** 点赞植物 */
    @PutMapping("/updatelikeCount/{plantId}")
    public void updateLikeCount(@PathVariable Long plantId) throws FloraException {
        plantService.updateLikeCount(plantId);
    }
    @GetMapping("/{userId}")
    public ResponseResult getPlants(@PathVariable long userId,
                                    @RequestParam(defaultValue = "0")Integer pageNum,
                                    @RequestParam(defaultValue = "10")Integer pageSize
    ){
        return ResponseResult.okResult(plantService.getPlantsByUserId(userId,pageNum,pageSize));
    }

    @GetMapping("/getPlantById/{plantId}")
    public ResponseResult getPlantById(@PathVariable Long plantId){
        return ResponseResult.okResult(plantService.getPlantById(plantId));
    }

    @GetMapping("/getPlants")
    public ResponseResult selectPlants( @RequestParam(required = false) String plantName,
                                        @RequestParam(required = false) Long categoryId,
                                        @RequestParam(required = false,defaultValue = "0") Integer pageNum,
                                        @RequestParam(required = false,defaultValue = "12") Integer pageSize){
        Map<String,Object> condition = new HashMap<>();
        if(plantName!=null&&plantName!=""){
            condition.put("plant_name",plantName);
        }
        if(categoryId!=null&&categoryId!=0){
            condition.put("category_id",categoryId);
        }
        return ResponseResult.okResult(plantService.getPlants(condition,pageNum,pageSize));
    }

    @GetMapping("/hotPlants")
    public ResponseResult getHotPlants(){
        return ResponseResult.okResult(plantService.getHotPlants());

    }

    @GetMapping("/newPlants")
    public IPage<Plant> getNewPlants(@RequestParam(defaultValue = "0") Integer pageNum,
                                     @RequestParam(defaultValue = "10") Integer pageSize){
        return plantService.getNewPlants(pageNum,pageSize);
    }

    @DeleteMapping("/{plantId}")
    public void deletePlant(@PathVariable long plantId){
        plantService.removeById(plantId);
    }

    @PostMapping("/addPlant")
    public Plant addPlant(@RequestBody Plant plant)throws FloraException {
        return plantService.addPlant(plant);
    }
    @PostMapping("/uploadPhoto/{plantId}")
    public String uploadPhotoOfPlant(@RequestBody MultipartFile img,@PathVariable long plantId)throws FloraException{
        return plantService.uploadPhotoOfPlant(img,plantId);
    }
    @PutMapping("/update/{plantId}")
    public void updatePlant(@RequestBody Plant plant, @PathVariable long plantId)throws FloraException{
        plantService.updatePlant(plantId,plant);
    }

    @DeleteMapping("/delete/{id}")
    public void deletePlant(@PathVariable Long id){
        plantService.deletePlant(id);
    }
}

